
*** merge census 1982 and 1990
use ../data/census1990hh, clear
g wave = 1990

append using ../data/census1982hh
replace wave = 1982 if missing(wave)

rename hhcode hhcode_raw
egen hhcode = group(hhcode_raw wave)
drop hhcode_raw

*** sample restrictions
* province
drop if prov == 54 
* drop Tibet

tab prov wave
replace prov = 51 if prov == 50 
* merge Chongqing with Sichuan

replace prov = 44 if prov == 46 
* merge Hainan with Guangdong

* filter households
keep if nfather <= 1 & nmother <= 1

keep if nmother == 1

g han = hanP2
replace han = 0 if hanP1 == 0

drop if missing(han)

drop if ntwin > 2
drop if ntriplet > 0
drop if nquadruplet == 4

* coresidence
drop if sampleChCoresideMoreThanBirth == 1
keep if sampleChCoreside == 1

* rural households
tab hukouP2 wave, m

g urban90 = hukouP2 if wave == 1990
replace urban90 = hukouP1 if wave == 1990 & missing(urban90)

g urban82 = 1 - agriempP2 if wave == 1982
replace urban82 = 1 - agriempP1 if wave == 1982 & missing(urban82)

g urban = urban90
replace urban = urban82 if wave == 1982
tab urban, m


* child education
recode eduyC1 (0=1)(6=2)(9=3)(12 13 14 15 16 19 22 = 4), g(edulevC1)
tab edulevC1 eduyC1


forvalues i = 2/4 {
	g edulev`i'C1 = edulevC1 >= `i'
	replace edulev`i'C1 = . if missing(edulevC1)
}

sum edulev*C1


* child age
sum ageC1, d
keep if ageC1 <= 17
keep if ageC1 >= 6

* mother cohort
keep if birthyP2 >= 1931 & birthyP2 <= 1970

* keep variables
local idvars "wave hhcode prov urban "
local hhvars "nchild nmale ntwin twinhh nmaletwin twinorder minchildage maxchildage sample1plus sample2plus sample3plus sample4plus sample5plus "
local fmvars "birthyP1 ageP1 eduyP1 hanP1  birthyP2 ageP2 eduyP2 hanP2 han chbornP2  age1birthP2  age2birthP2 age3birthP2 age4birthP2 age5birthP2 age6birthP2  "
local cvars "maleC1 birthyC1 ageC1 eduyC1 edusC1 edulevC1 edulev2C1 edulev3C1 edulev4C1  maleC2 birthyC2 ageC2 eduyC2   maleC3 birthyC3 ageC3 eduyC3 "


keep `idvars' `hhvars' `fmvars' `cvars'
order `idvars' `hhvars' `fmvars' `cvars'

* rename C1 variables
rename *C1 *


*** fertility at each margin
clonevar nchild0 = nchild

keep if nchild >= 0 & nchild <= 6

forvalues j = 1/6 {
	g nchild`j' = chbornP2 >= `j'
	label var nchild`j' "N >= `j'"
	* use chbornP2, which include non-coresiding children
}

sum chbornP2 nchild*

tabstat birthyP2, by(wave) s(mean sd n)

compress
save ../temp/c8290hh_premerge, replace



*** merge with policy measures

* fine at each birth by age at birth and spacing
forvalues i = 2/6 {
	use ../data/fineBirth, clear 
	* fineBirth: from Rfiles
	keep if birth == `i'
	drop birth
	rename fineBirth fine`i'b
	save ../temp/temp`i', replace
}


use ../temp/c8290hh_premerge, clear

* merge with birth at each birth
forvalues i = 2/6 {
	local j = `i' - 1
	g year`j'birthP2 = birthyP2 + age`j'birthP2
	rename year`j'birthP2 yearLastBirthP2
	merge m:1 prov yearLastBirthP2 using ../temp/temp`i'
	drop if _merge == 2
	drop _merge
	rename yearLastBirthP2 year`j'birthP2
	erase ../temp/temp`i'.dta
}


g edulevP1 = 1 if eduyP1 == 0
replace edulevP1 = 2 if eduyP1 == 6
replace edulevP1 = 3 if eduyP1 == 9
replace edulevP1 = 4 if eduyP1 >= 12
replace edulevP1 = . if missing(eduyP1)


g edulevP2 = 1 if eduyP2 == 0
replace edulevP2 = 2 if eduyP2 == 6
replace edulevP2 = 3 if eduyP2 == 9
replace edulevP2 = 4 if eduyP2 >= 12
replace edulevP2 = . if missing(eduyP2)

tab edulevP1 edulevP2, m


	* prov-cohort policy measures
merge m:1 prov birthyP2 using ../data/llfExposureWide

tab prov if _merge == 1
tab prov if _merge == 2

drop if _merge == 2
drop _merge


* merge with other variables

	* send-down
merge m:1 prov using ../temp/sdyReceivedRatio

tab prov _merge if _merge ~= 3

drop if _merge == 2
drop _merge

	* famine
merge m:1 prov using ../temp/census1990_famine
tab prov if _merge == 2
drop if _merge == 2
drop _merge

	* CR fatality
merge m:1 prov using ../temp/CRfatality

tab prov _merge if _merge ~= 3

drop if _merge == 2
drop _merge

	* minShare
merge m:1 prov using ../temp/census1990_minShare
tab prov _merge if _merge ~= 3
drop if _merge == 2
drop _merge

	* gdp, pop growth
merge m:1 prov using ../temp/provMacro
tab prov _merge if _merge ~= 3
drop if _merge == 2
drop _merge

	* number teachers in secondary school
rename birthy byear
merge m:1 prov byear using ../temp/provSecondarySchool
drop if _merge == 2
drop _merge
rename byear birthy 


	* compulsory education law, Ma Mingming cohort
merge m:1 prov using ../temp/provCEL
drop if _merge == 2
drop _merge

g cel = birthy >= cohort_cel

g age2 = age*age
g age3 = age2*age


g fine3b_twinhh = fine3b * twinhh

g fine3b_nchild3 = fine3b * nchild3

compress

save ../data/c8290hh, replace


*** erase temporary files to release space

erase ../temp/c8290hh_premerge.dta
erase ../temp/sdyReceivedRatio.dta
erase ../temp/census1990_famine.dta
erase ../temp/CRfatality.dta
erase ../temp/census1990_minShare.dta
erase ../temp/provMacro.dta
erase ../temp/provSecondarySchool.dta
erase ../temp/provCEL.dta

